package com.example.exam.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.exam.entity.User;
import com.example.exam.entity.UserRole;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author zhang
 */
@Mapper
public interface AdminMapper extends BaseMapper<User> {
    @Insert("insert into user_roles(user_id,role_id) values(#{user_id},#{role_id})")
    Integer insertRole(UserRole userRole);

    //批量保存用户的方法
    @Insert({
            "<script>",
            "INSERT INTO user (account, name, password, time, status, sex, age, email, salt, idcard, nickname) VALUES ",
            "<foreach collection='teacher' item='user' index='i' separator=','>",
            "(#{user.account},#{user.name},#{user.password}, #{user.time},#{user.stauts},#{user.sex},#{user.sex}," +
                    "#{user.age},#{user.email},#{user.salt},#{user.idcard},#{user.nickname})",
            "</foreach>",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer saveAllTeacher(@Param("teacher") List<User> users);

    //批量保存用户角色的方法
    @Insert({
            "<script>",
            "INSERT INTO user_roles (user_id,role_id) VALUES ",
            "<foreach collection='teacher' item='user' index='i' separator=','>",
            "(#{user_id},#{role_id})",
            "</foreach>",
            "</script>"
    })
    Integer saveAllUserRole(@Param("roles") List<UserRole> roles);
}
